<?php

namespace App\Http\Controllers\Admin;

use Auth, Hash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\Update_password;

class HomeController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_admin' => 'active open'
        ]);
    }

    public function index()
    {
        return view('admin.index');
    }


    /**
     * 加载修改密码页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function change_password()
    {
        return view('admin.change_password');
    }


    /**
     * 执行修改密码
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update_password(Update_password $request)
    {
        $user = Auth::user();

        if ($request->has('password') && $request->password != '') {
            if (!\Hash::check($request->oldpassword, $user->password)) {
                return back()->with('error', '原始密码输入错误~');
            }

            if ($request->password != $request->newpassword) {
                return back()->with('error', '两次密码输入不一致~');
            }
        }

        $user->password = bcrypt($request->input('password'));
        $user->email = $request->email;
        $user->mobile = $request->mobile;
        $user->save();

        return redirect(route('change_password'))->with('success', '您已成功修改密码~');
    }


}
